package com.dqhl.community.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.dqhl.community.R;
import com.dqhl.community.adapter.GoodsSelectRootListAdapter;
import com.dqhl.community.adapter.YingYangPinAdapter;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.Goods;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LIENLIN on 2016/9/20.
 */
public class YingYangPinActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private GridView mGvYingYangPin;
    private ImageView iv_topBack;
    private ImageView iv_topOperation;
    private TextView tv_topTitle;
    private List<Goods> selectResultGoodsList;
    private YingYangPinAdapter shoppingOnlineAdapter;
    private GoodsSelectRootListAdapter adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yingyangpin);
        initView();
        showSelectResult("40");
    }

    private void initView() {
        mGvYingYangPin = (GridView) findViewById(R.id.gv_yingyangpin);
        tv_topTitle = (TextView) findViewById(R.id.tv_top_title);
        iv_topBack = (ImageView) findViewById(R.id.iv_top_back);

        tv_topTitle.setText("营养保健");
        iv_topBack.setVisibility(View.VISIBLE);
//        mGvYingYangPin.setAdapter(new YingYangPinAdapter(this));
//        mGvYingYangPin.setOnItemClickListener(this);
        iv_topBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//        Bundle bundle = new Bundle();
//        bundle.putString("name", "0");
//        bundle.putString("price", "0");
//        bundle.putString("info", "0");
//        bundle.putString("number", "0");
//        overlay(ShopOnlinDetailActivityYingYang.class, bundle);
    }
    private void showSelectResult(String id) {
        showCustomLoadBar("加载中……");
        RequestParams params = new RequestParams(Config.Api.supermarket);
        params.addBodyParameter("son_id", id);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getInfo(result, "data");
                if (errCode == 0) {
                    selectResultGoodsList = new ArrayList<Goods>(
                            JSONArray.parseArray(data, Goods.class)
                    );
                    Dlog.e("selectResultGoodsList:" + selectResultGoodsList.toString());
                    shoppingOnlineAdapter = new YingYangPinAdapter(YingYangPinActivity.this,
                            selectResultGoodsList);
                    shoppingOnlineAdapter.setOnBuyClickListener(new YingYangPinAdapter.onBuyClickListener() {
                        @Override
                        public void doBuy(String goodsId, String strImg, String strName, String strPrice) {
                            Dlog.e("clicked goodsId:" + goodsId);
                            //跳到结算界面
                            Bundle bundle = new Bundle();
                            bundle.putString("total_money", "0");
                            bundle.putInt("number", 0);
                            bundle.putString("img", strImg);
                            bundle.putString("name", strName);
                            bundle.putString("goods_id", goodsId);
                            bundle.putString("price", strPrice);
                            overlay(ShoppingPaymentActivity.class, bundle);
                        }
                    });
                    shoppingOnlineAdapter.notifyDataSetChanged();
                    mGvYingYangPin.setAdapter(shoppingOnlineAdapter);
                    mGvYingYangPin.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            String goods_id = selectResultGoodsList.get(i).getId();
                            Dlog.e("传到详情页的goods_id为：" + goods_id);
                            Bundle bundle = new Bundle();
                            bundle.putString("goods_id", goods_id);
                            overlay(ShopOnlinDetailActivity.class, bundle);
                        }
                    });

                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }
}
